import 'dart:io';

import 'package:flutter/widgets.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:speak_text/main.dart';

late FlutterTts flutterTts;

Future<String> ttsInit() async {
  WidgetsFlutterBinding.ensureInitialized();
  String ret = 'do not support any language';
  if (!Platform.isAndroid) {
    return 'do not support any language';
  }
  flutterTts = FlutterTts();
  var defaultEngine = await flutterTts.getDefaultEngine;
  ret = defaultEngine.toString();
  var f = await flutterTts.isLanguageAvailable("zh-CN");
  if (f) {
    await flutterTts.setLanguage("zh-CN");
  } else {
    await flutterTts.setLanguage("en-US");
  }
  var voices = await flutterTts.getVoices;
  MyData.voices = voices;

  await flutterTts.setSpeechRate(1.0);

  await flutterTts.setVolume(1.0);

  await flutterTts.setPitch(1.0);
  return ret;
}

Future<void> speakText(String text) async {
    await flutterTts.speak(text);
    await flutterTts.awaitSpeakCompletion(true);
}

